package friot.utils.exception;

import friot.utils.constants.AppHttpCodeEnum;
import friot.utils.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author He Kuntao
 * @date 2023/11/24 17:20
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionCatch {

    /**
     * 处理不可控异常
     * 
     * @param e 异常对象
     * @return 返回结果
     */
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        log.error("catch exception:{}", "不可控异常" + e.getMessage());
        return Result.error(AppHttpCodeEnum.UNKNOWN_ERROR);
    }

    /**
     * 处理自定义业务异常
     *
     * @param e 异常对象
     * @return 返回结果
     */
    @ExceptionHandler(ServiceException.class)
    public Result exception(ServiceException e) {
        log.error("catch exception:{}", e.getAppHttpCodeEnum().getErrorMessage());
        return Result.error(e.getAppHttpCodeEnum());
    }

}
